package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* AutoRotateView
* */
class AutoRotateView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val degrees = floatArrayOf(0F, 180F)

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            strokeWidth = mStrokeWidth
            pathEffect = CornerPathEffect(mStrokeWidth * 2)
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_A93CD1)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            strokeWidth = mStrokeWidth
            style = Paint.Style.FILL_AND_STROKE
            color = context.resources.getColor(R.color.color_A93CD1)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)

            val rectRadioX = 5 / 14.0F
            val rectRadioY = 3 / 4.0F
            val rectBaseX = cx * rectRadioX
            val rectBaseY = cy * rectRadioY
            mPath0.apply {
                reset()
                moveTo(-rectBaseX, -rectBaseY)
                lineTo(-rectBaseX, rectBaseY)
                lineTo(rectBaseX, rectBaseY)
                lineTo(rectBaseX, -rectBaseY)
                close()
            }
            drawPath(mPath0, mPaint0)

            save()
            val arcRadio = 5 / 6.0F
            val arcBaseX = cx * arcRadio
            val arcBaseY = cy * arcRadio
            for (degree in degrees) {
                rotate(degree)
                drawArc(
                    -arcBaseX,
                    -arcBaseY,
                    arcBaseX,
                    arcBaseY,
                    135F,
                    90F,
                    false,
                    mPaint0
                )

                mPath1.apply {
                    moveTo(-cx * 19 / 24, cy / 5)
                    lineTo(-cx * 7 / 12, cy / 5)
                    lineTo(-cx * 7 / 12, cy * 3 / 5)
                    close()
                }
                drawPath(mPath1, mPaint1)
            }
            restore()
        }
    }
}